package com.mspbots.cw.amqp;

import com.mspbots.cw.amqp.model.TenantUser;
import com.mspbots.cw.amqp.model.UsersMapping;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;


/**
 * com.mspbots.core.amqp.receivers.TenantUserBaseReceiver
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2019/7/30
 */
public abstract class BaseUserReceiver extends BaseReceiver {

    protected Flux<TenantUser> getTenantUser(Integer tenantId, String email) {
        return this.databaseClient.execute("SELECT * FROM tenant_user WHERE tenant_id=:tenantId and email ilike :email")
                .as(TenantUser.class)
                .bind("tenantId", tenantId)
                .bind("email", email)
                .fetch().all();
    }

    protected Mono<UsersMapping> getUserMapping(Integer tenantId, Long userId) {
        return this.databaseClient.execute("SELECT * FROM tenant_user_mapping WHERE " +
                "tenant_id=:tenantId and tenant_user_id=:tenantUserId")
                .as(UsersMapping.class)
                .bind("tenantUserId", userId)
                .bind("tenantId", tenantId)
                .fetch().one();
    }

}
